package com.sky.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.sky.context.BaseContext;
import com.sky.entity.Dish;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.result.Result;
import com.sky.service.ReadExcelService;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

@Service
public class ReadExcelServiceImpl implements ReadExcelService {

    @Autowired
    private DishMapper dishMapper;
    @Override
    public Result readExcel(MultipartFile file) throws IOException {

        InputStream in = file.getInputStream();
        XSSFWorkbook workbook = new XSSFWorkbook(in);

        XSSFSheet sheet = workbook.getSheetAt(0);
        // 获取商品描述，并转换格式
        String descriptionStr = sheet.getRow(1).getCell(1).getStringCellValue();
        String[] split = descriptionStr.split("-");
        System.out.println(Arrays.toString(split));
        String y = split[0].replaceAll("年","");
        String m = split[1].replaceAll("月","");
        String d =  split[2].replaceAll("日","");
        String description = y+m+d;

        // 循环拿到所有的菜品信息
        List<Dish> dishList = new ArrayList<>();
        for (int i = 3; i < sheet.getLastRowNum(); i++) {
            String name = sheet.getRow(i).getCell(1).getStringCellValue();
            double price = sheet.getRow(i).getCell(2).getNumericCellValue();
            String category = sheet.getRow(i).getCell(3).getStringCellValue();
            String status = sheet.getRow(i).getCell(4).getStringCellValue();

            //TODO 根据分类名获取分类id

            Dish dish = Dish.builder()
                    .name(name)
                    .status(Integer.valueOf(1))
                    .categoryId(7l)
                    .price(BigDecimal.valueOf(price))
                    .description(description)
                    .createUser(BaseContext.getCurrentId())
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .updateUser(BaseContext.getCurrentId())
                    .image("https://web-itshuo.oss-cn-beijing.aliyuncs.com/dd3dbf92-6603-47bd-8725-44c0ea1eb447.png")
                    .build();
            dishList.add(dish);
        }
//        循环插入菜品数据
        for (Dish dish : dishList) {
            dishMapper.insert(dish);
        }
//        批量插入菜品数据
        dishMapper.insertBatch(dishList);

//         EasyExcel.read(file.getInputStream(),Dish.class,new PageReadListener<Dish>(new Consumer<List<Dish>>() {
//             @Override
//             public void accept(List<Dish> dishes) {
////                 dishMapper.insertBatch(dishes);
//                 System.out.println(dishes);
//             }
//         })).sheet().doRead();
        in.close();
        workbook.close();

        return Result.success();
    }



}
